package com.llb.utils;

import com.llb.entity.*;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PropertiesUtil
 * @Description TODO
 * @Author liang
 * @Date 2019/3/26 17:00
 * @Version 1.0
 **/
public class DMUtil {

    private static ArrayList<String> appIdList;
    private static ArrayList<String> xzlx_dmList;
    private static ArrayList<String> xzqhList;
    private static ArrayList<String> sbjbjgList;

    private  static DMUtil INSTANCE  ;

    /**
     * 饿汉式 只加载一次代码表
     */
    static {
        INSTANCE = new DMUtil();
    }
    /**
     * 获代码表map
     * @return
     * @throws IOException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    private  DMUtil () {
        String xlsFile = "!/dm.xlsx";
        URL url = null;
        try {
            url = new URL("jar:file:" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()+xlsFile);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStream inputStream = null;
        try {
            inputStream = url.openStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ExcelReader excelReader = new ExcelReader();
        Map<String, List<Object>>  dmMap = null;
        try {
            dmMap = excelReader.readExcelContent(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //从map中获取每张码表的list
        List<Object> apps = dmMap.get("CS_SBF_APP");
        List<Object> xzlxs = dmMap.get("DM_SBF_RS_XZLX");
        List<Object> xzqhs = dmMap.get("DM_SBF_RS_XZQH");
        List<Object> sbjbjgs = dmMap.get("DM_SBF_SBJBJG");

        //遍历每个对象,取出有效数据,得到关键字段
        //appid
         appIdList = new ArrayList<String>();
        for(Object obj:apps){
            CS_SBF_APP app = (CS_SBF_APP) obj;
            //过滤有效标志
            if(app.getYXBZ().equals("Y")){
                appIdList.add(app.getAPP_ID());
            }
        }

        //险种类型XZLX_DM
        xzlx_dmList = new ArrayList<String>();
        for(Object obj:xzlxs){
            DM_SBF_RS_XZLX xzlx = (DM_SBF_RS_XZLX) obj;
            //过滤有效标志
            if(xzlx.getYXBZ().equals("Y")){
                xzlx_dmList.add(xzlx.getXZLX_DM());
            }
        }

        //行政区划,XZQHSZ_DM
         xzqhList = new ArrayList<String>();
        for(Object obj:xzqhs){
            DM_SBF_RS_XZQH xzqh = (DM_SBF_RS_XZQH) obj;
            //过滤有效标志
            if(xzqh.getYXBZ().equals("Y")){
                xzqhList.add(xzqh.getXZQHSZ_DM());
            }
        }

        //社保经办机构,SBJBJG_DM
         sbjbjgList = new ArrayList<String>();
        for(Object obj:sbjbjgs){
            DM_SBF_SBJBJG sbjbjg = (DM_SBF_SBJBJG) obj;
            //过滤有效标志
            if(sbjbjg.getYXBZ().equals("Y")){
                sbjbjgList.add(sbjbjg.getSBJBJG_DM());
            }
        }

    }


    public static  boolean checkDM(String value,Class clazz)   {
        String clazzName = clazz.getName();
        if(clazzName.equals(CS_SBF_APP.class.getName())){
            if(appIdList.contains(value)){
                return true;
            }
        }else if(clazzName.equals(DM_SBF_RS_XZLX.class.getName())){
            if(xzlx_dmList.contains(value)){
                return true;
            }
        }else if(clazzName.equals(DM_SBF_RS_XZQH.class.getName())){
            if(xzqhList.contains(value)){
                return true;
            }
        }else if(clazzName.equals(DM_SBF_SBJBJG.class.getName())){
            if(sbjbjgList.contains(value)){
                return true;
            }
        }

        return false;
    }

}
